import React, { useCallback, useEffect, useImperativeHandle, useRef, useState } from "react";
import { columnsData } from "./ColumnsData";
import { useModel } from "umi";
import { getQckmhdData } from '../service';
import FormsTitle from "../components/FormsTitle";
import notify from "devextreme/ui/notify";
import { DevGrid } from "~/public/cpas-ui";
import PageLoading from "@/pages/components/PageLoading";


const SubjectForms = React.forwardRef((props: any, ref) => {
    const { slectYearData } = props;
    const { Data_ID, checkBoxVaue } = slectYearData;
    const { initialState } = useModel('@@initialState');
    const { dbname } = initialState?.curPart as API.CurPart || {};
    const [qckmhdData, setQckmhdData] = useState<any[]>([]);
    const DataRef = useRef<any>();
    const [spinLoad, setSpinLoad] = useState<boolean>(false);


    function getGridInstance() {
        return DataRef.current.gridRef.current.instance;
    }

    const questQckmhdData = useCallback(async (tabs_year_ID: string, tabs_checkBox: boolean) => {
        setSpinLoad(true);
        const res: any = await getQckmhdData({
            dbname,
            data_ID: tabs_year_ID,
            wcybxs: tabs_checkBox
        });
        if (res.success) {
            getGridInstance().pageIndex(0);
            setQckmhdData(res.data);
        } else {
            notify(`获取数据失败：${res.data}`, "error", 2000);
        }
        setSpinLoad(false);
    }, []);


    // 自定义方法
    useImperativeHandle(ref, () => ({
        reloadDevTable: (tabs_year_ID: any, tabs_checkBox: boolean) => {
            questQckmhdData(tabs_year_ID, tabs_checkBox);
        },
    }));

    useEffect(() => {
        if (Data_ID) {
            questQckmhdData(Data_ID, checkBoxVaue);
        }
    }, [Data_ID, checkBoxVaue]);

    return (<div style={{ height: "calc(100% - 45px)" }}>
        {/* {spinLoad && <SpinLoading loadString="" />} */}
        <PageLoading
            isOpen={spinLoad}
            isMaskClose={false}
            content={""}
            width={200}
        />

        {/* <FormsTitle tabsData={{ tabsTitle: '期初科目核对' }}></FormsTitle> */}
        <DevGrid.RangeTap
            width="100%"
            height="100%"
            dataSource={qckmhdData}
            // rowAlternationEnabled={true}
            keyExpr="YSKMBH"
            className="devHeadersCenter PaperDevSortableGrid "
            ref={DataRef}
            defaultColumns={columnsData}
            sheetName="期初科目核对"
            excelName="期初科目核对"
            showBorders={true}
            showRowLines={true}
            showColumnLines={true}
            paging={{
                enabled: false
            }}
        />
    </div>
    )
})
export default SubjectForms;